package cn.edu.hitwh.resume.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.hitwh.resume.biz.service.ResumeService;
import cn.edu.hitwh.resume.biz.vo.ResumeVo;
import cn.edu.hitwh.resume.dal.dao.DisireDao;
import cn.edu.hitwh.resume.dal.dao.EducationDao;
import cn.edu.hitwh.resume.dal.dao.ProjectExperienceDao;
import cn.edu.hitwh.resume.dal.dao.SeekerDao;
import cn.edu.hitwh.resume.dal.dao.SelfDescDao;
import cn.edu.hitwh.resume.dal.dao.SkillDao;
import cn.edu.hitwh.resume.dal.dao.WorkExperienceDao;
import cn.edu.hitwh.resume.dal.entity.DisireDo;
import cn.edu.hitwh.resume.dal.entity.EducationDo;
import cn.edu.hitwh.resume.dal.entity.ProjectExperienceDo;
import cn.edu.hitwh.resume.dal.entity.SelfDescDo;
import cn.edu.hitwh.resume.dal.entity.SkillDo;
import cn.edu.hitwh.resume.dal.entity.WorkExperienceDo;
import cn.edu.hitwh.resume.web.controller.vo.ResultVo;

@RestController
public class ResumeController {
	
	@Autowired
	ResumeService resumeService;
	@Autowired
	SeekerDao seekerDao;
	@Autowired
	EducationDao euducationDao;
	@Autowired
	WorkExperienceDao workExperienceDao;
	@Autowired
	ProjectExperienceDao projectExperienceDao;
	@Autowired
	SelfDescDao selfDescDao;
	@Autowired
	SkillDao skillDao;
	@Autowired
	DisireDao disireDao;
	
	@RequestMapping(value = "/resume/list") 
	public List<ResumeVo> getAll() {
		return this.resumeService.findAll();
	}
	
	@RequestMapping(value = "/resume/seeker/{id}")
 	public ResumeVo getListBySeekerId(@PathVariable(value = "id") Integer id) {
		return this.resumeService.findBySeekerId(id);
	}
	
	// Education action
	@RequestMapping(value = "/resume/seeker/{seekerId}/education", method = RequestMethod.POST)
	public ResultVo addEducation(@RequestParam(value = "school") String school,
								  @RequestParam(value = "type") Integer type,
								  @RequestParam(value = "major") String major,
								  @RequestParam(value = "degree") Integer degree,
								  @RequestParam(value = "duration") String duration,
								  @PathVariable(value = "seekerId") Integer seekerId) {
		EducationDo education = new EducationDo();
		education.setSchool(school);
		education.setType(type);
		education.setMajor(major);
		education.setDegree(degree);
		education.setDuration(duration);
		education.setSeekerId(seekerId);
		this.euducationDao.create(education);
		return new ResultVo(true, null, education);
	}
	
	@RequestMapping(value = "/resume/education/id/{id}", method = RequestMethod.POST)
	public ResultVo editEducation(@PathVariable(value = "id") Integer id,
								  @RequestParam(value = "school") String school,
								  @RequestParam(value = "type") Integer type,
								  @RequestParam(value = "major") String major,
								  @RequestParam(value = "degree") Integer degree,
								  @RequestParam(value = "duration") String duration) {
		EducationDo education = new EducationDo();
		education.setId(id);
		education.setSchool(school);
		education.setType(type);
		education.setMajor(major);
		education.setDegree(degree);
		education.setDuration(duration);
		this.euducationDao.update(education);
		return new ResultVo(true, null, education);
	}
	
	@RequestMapping(value = "/resume/education/id/{id}", method = RequestMethod.DELETE)
	public ResultVo removeEducation(@PathVariable(value = "id") Integer id) {
		this.euducationDao.delete(id);
		return new ResultVo();
	}
	
	// WorkExperience action
	@RequestMapping(value = "/resume/seeker/{seekerId}/work", method = RequestMethod.POST)
	public ResultVo addWork(@RequestParam(value = "company") String company,
						     @RequestParam(value = "position") String position,
						     @RequestParam(value = "duration") String duration,
						     @RequestParam(value = "task") String task,
						     @PathVariable(value = "seekerId") Integer seekerId) {
		WorkExperienceDo work = new WorkExperienceDo();
		work.setCompany(company);
		work.setPosition(position);
		work.setDuration(duration);
		work.setTask(task);
		work.setSeekerId(seekerId);
		this.workExperienceDao.create(work);
		return new ResultVo(true, null, work);
	}
	
	@RequestMapping(value = "/resume/work/id/{id}", method = RequestMethod.POST)
	public ResultVo editWork(@PathVariable(value = "id") Integer id,
							  @RequestParam(value = "company") String company,
							  @RequestParam(value = "position") String position,
							  @RequestParam(value = "duration") String duration,
							  @RequestParam(value = "task") String task) {
		WorkExperienceDo work = new WorkExperienceDo();
		work.setId(id);
		work.setCompany(company);
		work.setPosition(position);
		work.setDuration(duration);
		work.setTask(task);
		this.workExperienceDao.update(work);
		return new ResultVo(true, null, work);
	}
	
	@RequestMapping(value = "/resume/work/id/{id}", method = RequestMethod.DELETE)
	public ResultVo removeWork(@PathVariable(value = "id") Integer id) {
		this.workExperienceDao.delete(id);
		return new ResultVo();
	}
	
	// ProjectExperience action
	@RequestMapping(value = "/resume/seeker/{seekerId}/project", method = RequestMethod.POST)
	public ResultVo addProject(@RequestParam(value = "proName") String proName,
						        @RequestParam(value = "proDesc") String proDesc,
						        @RequestParam(value = "position") String position,
						        @RequestParam(value = "duration") String duration,
						        @PathVariable(value = "seekerId") Integer seekerId) {
		ProjectExperienceDo project = new ProjectExperienceDo();
		project.setProName(proName);
		project.setProDesc(proDesc);
		project.setPosition(position);
		project.setDuration(duration);
		project.setSeekerId(seekerId);
		this.projectExperienceDao.create(project);
		return new ResultVo(true, null, project);
	}
	
	@RequestMapping(value = "/resume/project/id/{id}", method = RequestMethod.POST)
	public ResultVo editProject(@PathVariable(value = "id") Integer id,
								@RequestParam(value = "proName") String proName,
						        @RequestParam(value = "proDesc") String proDesc,
						        @RequestParam(value = "position") String position,
						        @RequestParam(value = "duration") String duration) {
		ProjectExperienceDo project = new ProjectExperienceDo();
		project.setId(id);
		project.setProName(proName);
		project.setProDesc(proDesc);
		project.setPosition(position);
		project.setDuration(duration);
		this.projectExperienceDao.update(project);
		return new ResultVo(true, null, project);
	}
	
	@RequestMapping(value = "/resume/project/id/{id}", method = RequestMethod.DELETE)
	public ResultVo removeProject(@PathVariable(value = "id") Integer id) {
		this.projectExperienceDao.delete(id);
		return new ResultVo();
	}
	
	// SelfDesc action
	@RequestMapping(value = "/resume/seeker/{seekerId}/self", method = RequestMethod.POST)
	public ResultVo addSelfDesc(@RequestParam(value = "content") String content,
						        @PathVariable(value = "seekerId") Integer seekerId) {
		SelfDescDo self = new SelfDescDo();
		self.setContent(content);
		self.setSeekerId(seekerId);
		this.selfDescDao.create(self);
		return new ResultVo(true, null, self);
	}
	
	@RequestMapping(value = "/resume/self/id/{id}", method = RequestMethod.POST)
	public ResultVo editSelfDesc(@PathVariable(value = "id") Integer id,
								  @RequestParam(value = "content") String content) {
		SelfDescDo self = new SelfDescDo();
		self.setId(id);
		self.setContent(content);
		this.selfDescDao.update(self);
		return new ResultVo(true, null, self);
	}
	
	@RequestMapping(value = "/resume/self/id/{id}", method = RequestMethod.DELETE)
	public ResultVo removeSelfDesc(@PathVariable(value = "id") Integer id) {
		this.selfDescDao.delete(id);
		return new ResultVo();
	}
	
	// Skill action
	@RequestMapping(value = "/resume/seeker/{seekerId}/skill", method = RequestMethod.POST)
	public ResultVo addSkill(@RequestParam(value = "name") String name,
						      @RequestParam(value = "level") Integer level,
						      @PathVariable(value = "seekerId") Integer seekerId) {
		SkillDo skill = new SkillDo();
		skill.setName(name);
		skill.setLevel(level);
		skill.setSeekerId(seekerId);
		this.skillDao.create(skill);
		return new ResultVo(true, null, skill);
	}
	
	@RequestMapping(value = "/resume/skill/id/{id}", method = RequestMethod.POST)
	public ResultVo editSkill(@PathVariable(value = "id") Integer id,
							   @RequestParam(value = "name") String name,
							   @RequestParam(value = "level") int level) {
		SkillDo skill = new SkillDo();
		skill.setId(id);
		skill.setName(name);
		skill.setLevel(level);
		this.skillDao.update(skill);
		return new ResultVo(true, null, skill);
	}
	
	@RequestMapping(value = "/resume/skill/id/{id}", method = RequestMethod.DELETE)
	public ResultVo removeSkill(@PathVariable(value = "id") Integer id) {
		this.skillDao.delete(id);
		return new ResultVo();
	}
	
	// Disire action
	@RequestMapping(value = "/resume/seeker/{seekerId}/disire", method = RequestMethod.POST)
	public ResultVo addDisire(@RequestParam(value = "position") String position,
							   @RequestParam(value = "city") String city,
							   @RequestParam(value = "type") Integer type,
						       @RequestParam(value = "salary") Integer salary,
						       @RequestParam(value = "intentDate") String intentDate,
						       @PathVariable(value = "seekerId") Integer seekerId) {
		DisireDo disire = new DisireDo();
		disire.setPosition(position);
		disire.setCity(city);
		disire.setType(type);
		disire.setSalary(salary);
		disire.setIntentDate(intentDate);
		disire.setSeekerId(seekerId);
		this.disireDao.create(disire);
		return new ResultVo(true, null, disire);
	}
	
	@RequestMapping(value = "/resume/disire/id/{id}", method = RequestMethod.POST)
	public ResultVo editDisire(@PathVariable(value = "id") Integer id,
							   @RequestParam(value = "position") String position,
							   @RequestParam(value = "city") String city,
							   @RequestParam(value = "type") Integer type,
						       @RequestParam(value = "salary") Integer salary,
						       @RequestParam(value = "intentDate") String intentDate) {
		DisireDo disire = new DisireDo();
		disire.setId(id);
		disire.setPosition(position);
		disire.setCity(city);
		disire.setType(type);
		disire.setSalary(salary);
		disire.setIntentDate(intentDate);
		this.disireDao.update(disire);
		return new ResultVo(true, null, disire);
	}
	
	@RequestMapping(value = "/resume/disire/id/{id}", method = RequestMethod.DELETE)
	public ResultVo removeDisire(@PathVariable(value = "id") Integer id) {
		this.disireDao.delete(id);
		return new ResultVo();
	}
								  
	
}
